# coding: utf-8

from django.http import HttpResponseRedirect, HttpResponse
from djangoext.mako import render_to_response , render_to_string, render_string

import time, datetime
import re
import cStringIO
import os
import random
import types, operator
from shangjie.utils import log, cache
from shangjie.utils import lunar

from shangjie.conf import settings
from sqlalchemy.sql import *

from functools import partial

from pyExcelerator import *


import copy
from djangoext.urls import expose
from oa.utils import *

def selcomplaint(req):
    return render_to_response('CMS/complain/complainlist.html')

def selecomplaint(req):
    se = settings.DB_SESSION()
    try:
        # 获取请求对象
        flag, reqobj = get_reqobj(req)
        if not flag:
            return HttpResponse(
                sjapi.jsondumps({'rspcode': 8004, 'rspinfo': reqobj}, ensure_ascii=False, encoding='utf-8'),
                content_type='application/json; charset=utf-8')
        # 获取请求要素
        pcid = req.session['pcid']
        # pcid = 'PCID2016042300136147'
        page = int(reqobj.get('strPage', 1))
        start = (page - 1) * 10
        end = start + 10
        se = settings.DB_SESSION()
        row = []
        content = {}
        # 0未批复 1 已批复 -1 已删除
        sel_sql = """select * from zhsq_complainpraise  where  fatherid = '0' and  status != '-1' and companyId = '%s' order by createtime desc""" % pcid
        informations = se.execute(sel_sql.decode('utf-8')).fetchall()
        for result in informations:
            community_sql = """select comName from zhsq_community where communityVettedId ='%s'""" % result.cid
            comName = se.execute(community_sql.decode('utf-8')).fetchone().comName
            sql_select = """select pName  from zhsq_owner where pid = '%s' """ % result.userid
            pName = se.execute(sql_select.decode('utf-8')).fetchone().pName
            if not pName:
                sql_select = """select eName  from zhsq_employee where eid = '%s' """ % result.userid
                pName = se.execute(sql_select.decode('utf-8')).fetchone().eName
            sql_type = """select typename  from zhsq_complainpraisetype where id = '%s' """ % result.type
            typename = se.execute(sql_type.decode('utf-8')).fetchone().typename
            content = {'strId': str(result.id), 'strContent': result.content,
                           'strUserName': pName,
                           'strCreatetime': result.createtime,
                           'strType': typename, 'strComName': comName,'strImg01':result.img01.strip('/') if result.img01 else '','strImg02':result.img02.strip('/') if result.img02.strip('/') else '','strImg03':result.img03.strip('/') if result.img03 else ''}
            row.append(content)
            content = {}
        if len(row[start:end]) < 10:
            nextpage = '0'
        else:
            nextpage = '1'
        se.close()
        return HttpResponse(
            sjapi.jsondumps({'rspcode': 0, 'rspinfo': 'ok', 'data': row[start:end], 'nextpage': nextpage},
                            ensure_ascii=False, encoding='utf-8'),
            content_type='application/json; charset=utf-8')
    except Exception as e:
        se.close()
        return HttpResponse(
            sjapi.jsondumps({'rspcode': 8999, 'rspinfo': '服务器请求异常'}, ensure_ascii=False, encoding='utf-8'),
            content_type='application/json; charset=utf-8')


def delecomplaint(req):
    se = settings.DB_SESSION()
    try:
        # 获取请求对象
        flag, reqobj = get_reqobj(req)
        if not flag:
            return HttpResponse(
                sjapi.jsondumps({'rspcode': 8004, 'rspinfo': reqobj}, ensure_ascii=False, encoding='utf-8'),
                content_type='application/json; charset=utf-8')
        # 获取请求要素
        # 删除批评表扬的id
        id = reqobj.get('strId')
        sql_select = """select *  from zhsq_complainpraise where id = '%s'""" % id
        typeinfo = se.execute(sql_select.decode('utf-8')).fetchone()
        if not typeinfo:
            return HttpResponse(
                sjapi.jsondumps({'rspcode': 8004, 'rspinfo': reqobj}, ensure_ascii=False, encoding='utf-8'),
                content_type='application/json; charset=utf-8')
        id = typeinfo.id
        sql_sel = """select *  from zhsq_complainpraise where path like '%{}%'""".format(id)
        res = se.execute(sql_sel.decode('utf-8')).fetchall()
        if res:
            for r in res:
                update_sql = """update zhsq_complainpraise set status ='-1'where id='%s'""" % (r.id)
                se.execute(update_sql.decode('utf-8'))
        update_sql = """update zhsq_complainpraise set status ='-1'where id='%s'""" % (id)
        se.execute(update_sql.decode('utf-8'))
        se.close()
        return HttpResponse(
            sjapi.jsondumps({'rspcode': 0, 'rspinfo': 'ok'}, ensure_ascii=False, encoding='utf-8'),
            content_type='application/json; charset=utf-8')
    except Exception as e:
        se.close()
        return HttpResponse(
            sjapi.jsondumps({'rspcode': 8004, 'rspinfo': '服务器请求异常'}, ensure_ascii=False, encoding='utf-8'),
            content_type='application/json; charset=utf-8')



